import {
	list_by_user
} from '@/api/system/department.js'
import {
	setToken,
	setType,
	getCurDepartment,
	setCurDepartment,
	getCurCompany,
	setCurCompany
} from '@/utils/auth'
import logoSrc from '@/assets/images/logo.png'

const state = {
	company_info: getCurCompany(),
	curDepartment: getCurDepartment(), 
	departmentList: [] //所有公司部门职位，以及超管后台
}

const mutations = {
	SET_COMPANY_INFO: (state, info) => {
		info.logo = info.logo || ''
		state.company_info = info
		setCurCompany(info)
	},
	RESET_COMPANY_INFO: (state) => {
		state.company_info = {
			logo: 'https://fuxing-haohujia-test1.oss-cn-shanghai.aliyuncs.com/20210517094931986208.jpg',
			name: '美莱文化'
		}
	},
	SET_DEPARTMENT: (state, curDepartment) => {
		state.curDepartment = curDepartment
		setCurDepartment(curDepartment)
	},
	SET_DEPARTMENT_LIST: (state, departmentList) => {
		state.departmentList = departmentList
	}
}

const actions = {
	getDepartments({
		commit
	}) {
		return new Promise((resolve, reject) => {
			list_by_user().then(response => {
				const {
					data
				} = response
				
				if (!data) {
					reject('查询所属部门失败！')
				}else{
					commit('SET_DEPARTMENT_LIST', data || [])
					if(data && data.length > 0){
						commit('SET_DEPARTMENT', data[0].id)
					}
				}
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
